package com.htao.train.common.interceptor;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.htao.train.common.context.LoginMemberContext;
import com.htao.train.common.resp.MemberLoginResp;
import com.htao.train.common.util.JwtUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * @Author： HTTT
 * @Date： 2024/1/5 21:15
 * @Describe：
 */
@Slf4j
@Component
public class MemberInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("进入 MemberInterceptor");
        String token = request.getHeader("token");
        if(!StrUtil.isBlank(token)){
            log.info("获取会员登录token：{}", token);
            JSONObject jsonObject = JwtUtil.getJSONObject(token);
            log.info("当前登录会员：{}", jsonObject);
            MemberLoginResp memberLoginResp = JSONUtil.toBean(jsonObject, MemberLoginResp.class);
            LoginMemberContext.setMember(memberLoginResp);
        }
        return true;
    }
}
